
import axios from 'axios';
import {useAuthStore} from "@/store/modules/auth.js";

const service = axios.create({
    baseURL: 'https://xjg.tongguantech.cn',
    timeout: 5000, // 请求超时时间
    withCredentials: true,
});

// 获取指定名称的cookie值
function getCookie(name)
{
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    console.log(value);
    console.log(parts);
    if (parts.length === 2) {
        return parts.pop().split(';').shift();
    }
    return null;
}
// 请求拦截器
service.interceptors.request.use(
    config => {
        // const authStore = useAuthStore();
        // const token = authStore.satoken;
        // if (token)
        // {
        //     config.headers['satoken'] = token; // 或Authorization
        // }
        return config;
    },
    error => {
        // 处理请求错误
        //console.log(error);
        return Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response =>
    {
        const res = response.data;
        console.log(res);
        if (res.content !== "success")
            {
                console.log('请求失败:', res.message);
                return Promise.reject(new Error(res.message || 'Error'));
            }
        else
            {
                return res;
            }
    },
    error => {
        // 处理响应错误
        console.log('err' + error);
        return Promise.reject(error);
    }
);

export default service;